package com.atguigu.gulimall.order.controller;

import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.entity.OrderReturnReasonEntity;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.UUID;

@Controller
public class RabbitmqController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @ResponseBody
    @GetMapping("/sendMsg")
    public String sendMsg(@RequestParam(value = "num",defaultValue = "10") Integer num){
        for (int i = 0; i < num; i++) {
            if (i%2==0){
                OrderReturnReasonEntity orderReturnReasonEntity = new OrderReturnReasonEntity();
                orderReturnReasonEntity.setId((long) i);
                orderReturnReasonEntity.setCreateTime(new Date());
                orderReturnReasonEntity.setName("hh:"+i);
                CorrelationData correlationData = new CorrelationData(i+""+UUID.randomUUID().toString());
                rabbitTemplate.convertAndSend("hello.java.exchange","hello-java",orderReturnReasonEntity,correlationData);
            }else {
                OrderEntity orderEntity = new OrderEntity();
                orderEntity.setOrderSn(UUID.randomUUID().toString());
                CorrelationData correlationData = new CorrelationData(i+""+UUID.randomUUID().toString());
                rabbitTemplate.convertAndSend("hello.java.exchange","hello-java",orderEntity,correlationData);
            }

        }
        return "ok";
    }

}
